package com.wchat.controller;


import com.wchat.converter.ImportentSiteDTOConverter;
import com.wchat.dataobject.DateSiteEntity;
import com.wchat.dataobject.District;
import com.wchat.dataobject.LocationEntity;
import com.wchat.dto.ImportentSiteDTO;
import com.wchat.enums.Log;
import com.wchat.enums.ResultEnum;
import com.wchat.exceptions.ServiceException;
import com.wchat.repository.DateSiteRepository;
import com.wchat.repository.DistrictRepository;
import com.wchat.repository.LocationReposity;
import com.wchat.utils.ResultVoUtils;
import com.wchat.vo.ResultVo;
import com.wchat.vo.SecondRegionIdVo;
import com.wchat.vo.SiteDetailsVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RestController
@Api("约会场所基础服务")
@RequestMapping(name = "/date")
@Slf4j
public class DateSiteController {


    @Autowired private LocationReposity locationReposity;
    @Autowired private DateSiteRepository dateSiteRepository;
    @Autowired private DistrictRepository districtRepository;

    @ApiOperation(value = "获取第一级别行政区域所有列表",notes = "默认参数传空")
    @RequestMapping(value = "/firstRegion",method = RequestMethod.POST)
    public ResultVo<List<LocationEntity>> getFirstRegion(){
       Integer parentId = 0;
       return ResultVoUtils.success(locationReposity.findByParentId(parentId));
    }

    @Log("获取第二级别行政区域所有列表")
    @ApiOperation(value = "获取第二级别行政区域所有列表",notes = "参数获取时候传入第一行政区域的id")
    @RequestMapping(value = "/secondRegion",method = RequestMethod.POST)
    public ResultVo<List<LocationEntity>> getSecondRegion(@RequestBody Integer parentId){
        return ResultVoUtils.success(locationReposity.findByParentId(parentId));
    }

    @Log("获取第三级别行政区域所有列表")
    @ApiOperation(value = "获取第三级别行政区域所有列表",notes = "参数传入第二级别的id")
    @RequestMapping(value = "/thirdRegion",method = RequestMethod.POST)
    public ResultVo<List<LocationEntity>> getThirdRegion(@RequestBody Integer parentId){
        return ResultVoUtils.success(locationReposity.findByParentId(parentId));
    }

    @Log("获取列重点推荐约会场所列表")
    @ApiOperation(value = "获取列重点推荐约会场所列表",notes = "参数传入默认为空 默认首先显示重点推荐场所")
    @RequestMapping(value = "/importentSite",method = RequestMethod.POST)
    public ResultVo<List<ImportentSiteDTO>> getImportentSite(HttpServletRequest request){
        List<DateSiteEntity> dateSiteEntities = dateSiteRepository.findAll().stream()
                .filter(dateSiteEntity -> dateSiteEntity.getIsImportant() == 1)
                .collect(Collectors.toList());
        return ResultVoUtils.success(ImportentSiteDTOConverter.importentSiteDTOConverter(dateSiteEntities));
    }

    @ApiOperation(value = "获取约会场所详情",notes = "参数传入场所id")
    @RequestMapping(value = "/siteDetails",method = RequestMethod.POST)
    public ResultVo<DateSiteEntity> getSiteDetails(@RequestBody SiteDetailsVo siteDetailsVo, HttpSession  session){
        return ResultVoUtils.success(dateSiteRepository.findOne(siteDetailsVo.getSiteID()));
    }

    @ApiOperation(value = "根据三级联动地址选择约会场所",notes = "参数传入场所id")
    @RequestMapping(value = "/getSiteByRegion",method = RequestMethod.POST)
    public ResultVo<List<ImportentSiteDTO>> getSiteByRegion(@RequestBody SecondRegionIdVo secondRegionIdVo, HttpSession  session){
        List<District> locationEntity =  districtRepository.findByCode(secondRegionIdVo.getSecondRegionId());
        if(CollectionUtils.isEmpty(locationEntity) ||  StringUtils.isBlank(locationEntity.get(0).getName())){
            return ResultVoUtils.error(5003,"您所查询的区域未存在");
        }
        List<DateSiteEntity> dateSiteEntities = dateSiteRepository.findByRegion(locationEntity.get(0).getName());
        if(CollectionUtils.isEmpty(dateSiteEntities)){
            return ResultVoUtils.success("敬请期待，附近店铺正在入驻");
        }

        log.info("[dataentiry]",dateSiteEntities.get(0).getSiteName());
        return ResultVoUtils.success(ImportentSiteDTOConverter.importentSiteDTOConverter(dateSiteEntities));
    }

}
